﻿#include "CWorkMngrBase.h"

CWorkMngrBase::CWorkMngrBase()
{
}

CWorkMngrBase::~CWorkMngrBase()
{
}

void CWorkMngrBase::init(QString sSceneGuid, CDevBase * pIFDev, CRBaseData * pRBasicData)
{
}

void CWorkMngrBase::taskScan()
{
}

bool CWorkMngrBase::isTaskExec(int nType, int nCycType, QString sTime)
{
	if (nType == 1)//周期任务
	{
		if (nCycType == 5)//每半小时
		{
			QString sDate = QDateTime::currentDateTime().toString("yyyy/MM/dd hh");
			QString sMintu = QDateTime::currentDateTime().toString("mm");
			QString sTime = "";
			if (sMintu.toInt() >= 30)
			{
				sTime = sDate + ":30:00";
			}
			else
			{
				sTime = sDate + ":59:59";
			}
			QDateTime dDay = QDateTime::fromString(sTime, "yyyy/MM/dd hh:mm:ss");
			int minutes = abs(QDateTime::currentDateTime().secsTo(dDay));
			if (minutes > 60)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		else
		{
			QString sDate = QDateTime::currentDateTime().toString("yyyy/MM/dd hh");
			QString sTime = sDate + ":00:00";
			QDateTime dDay = QDateTime::fromString(sTime, "yyyy/MM/dd hh:mm:ss");
			int seconds = abs(QDateTime::currentDateTime().secsTo(dDay));
			QString sHour = QDateTime::currentDateTime().toString("hh");
			if (seconds > 60)return false;
			switch (nCycType)
			{
			case 0:  //12小时
			{
				int nHour = sHour.toInt() % 12;
				if (nHour == 0)return true;
				break;
			}
			case 1:  //6小时
			{
				int nHour = sHour.toInt() % 6;
				if (nHour == 0)return true;
				break;
			}
			case 2:  //4小时
			{
				int nHour = sHour.toInt() % 4;
				if (nHour == 0)return true;
				break;
			}
			case 3:  //2小时
			{
				int nHour = sHour.toInt() % 2;
				if (nHour == 0)return true;
				break;
			}
			case 4:  //1小时
			{
				return true;
				break;
			}
			default:
				return false;
				break;
			}
			return false;
		}
	}
	else //定时任务或立即执行
	{
		QDateTime dDateTime = QDateTime::fromString(sTime, "yyyy/MM/dd hh:mm:ss");
		QDateTime dCurrTime = QDateTime::currentDateTime();
		QString sTime = dCurrTime.toString("yyyy/MM/dd") + " " + dDateTime.toString("hh:mm:ss");
		QDateTime dDay = QDateTime::fromString(sTime, "yyyy/MM/dd hh:mm:ss");
		int seconds = abs(dCurrTime.secsTo(dDay));
		if (seconds < 60)return true;
		else return false;
	}
}

bool CWorkMngrBase::isTaskExec(QString sTime)
{
	QTime dTime = QDateTime::fromString(sTime, "yyyy/MM/dd hh:mm:ss").time();  //任务开始时间
	QTime dCurrTime = QTime::currentTime();
	if (dTime < dCurrTime)
	{
		return true;
	}
	else
	{
		return false;
	}
}

//存在立即执行任务 当前时间之后的都认为是待执行任务
bool CWorkMngrBase::compareTime(QString &sTime, int nType)
{
	//定时任务和时间比较
	if (nType == 0)
	{
		QTime dTime = QDateTime::fromString(sTime, "yyyy/MM/dd hh:mm:ss").time();
		QTime dCurrTime = QTime::currentTime();
		if (dTime >= dCurrTime)
		{
			sTime = QDateTime::currentDateTime().toString("yyyy/MM/dd ") + dTime.toString("hh:mm:ss");
			return true;
		}
		else
		{
			return false;
		}

	}
	//立即执行任务和完整的年月日时分秒比较
	else
	{
		QDateTime dTime = QDateTime::fromString(sTime, "yyyy/MM/dd hh:mm:ss");
		QDateTime dCurrTime = QDateTime::currentDateTime().addSecs(-7);
		if (dTime > dCurrTime)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}

/**
* @ Author: Gaojia
* @ Description:  超过任务开始 就认为任务过期了
*/
bool CWorkMngrBase::compareTimeOverdue(QString sTime)
{
	QTime dTime = QDateTime::fromString(sTime, "yyyy/MM/dd hh:mm:ss").time();  //任务开始时间
	QTime dCurrTime = QTime::currentTime();
	if (dTime < dCurrTime)   //超过任务开始时间+  就认为任务过期
	{
		return true;
	}
	else
	{
		return false;
	}
}


//周期任务 在当前时间之后的任务都是待执行
bool CWorkMngrBase::compareTimeEx(int nTime, QString &sReTime)
{
	// 定义特定的时
	QTime specificTime(nTime / 2, (nTime % 2) * 30, 0);
	// 将当前日期和特定时间拼接在一起
	QTime dCurrTime = QTime::currentTime();
	if (specificTime >= dCurrTime)
	{
		// 获取当前日期
		QDate currentDate = QDate::currentDate();
		// 将当前日期和特定时间拼接在一起
		QDateTime specificDateTime(currentDate, specificTime);
		sReTime = specificDateTime.toString("yyyy/MM/dd hh:mm:ss");
		return true;
	}
	else
	{
		return false;
	}
}